<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <!--
        
        
        
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <el-date-picker
          v-model="query.createTime"
          :default-time="['00:00:00','23:59:59']"
          type="daterange"
          range-separator=":"
          size="small"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="createTimeStart"
          end-placeholder="createTimeEnd"
        />
        <el-date-picker
          v-model="query.updateTime"
          :default-time="['00:00:00','23:59:59']"
          type="daterange"
          range-separator=":"
          size="small"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="updateTimeStart"
          end-placeholder="updateTimeEnd"
        />
        -->

        <rrOperation :crud="crud" />



      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="800px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">

        <!--
          <el-form-item label="主键id">
              <el-input v-model="form.id" style="width: 370px;" />
            </el-form-item>
            <el-form-item label="类型外键" prop="typeId">
              <el-input v-model="form.typeId" style="width: 370px;" />
            </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker v-model="form.createTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="编辑时间">
            <el-date-picker v-model="form.updateTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="删除(0存/1删)">
            <el-input v-model="form.isDel" style="width: 370px;" />
          </el-form-item>
        
        -->

          
          <el-form-item label="标题" prop="heards">
            <el-input v-model="form.heards" style="width: 666px;" />
          </el-form-item>
          <el-form-item label="副标题">
            <el-input v-model="form.subHeards" style="width: 666px;" />
          </el-form-item>
          <el-form-item label="类型" >
            <el-select v-model="form.typeId" placeholder="请选择" style="width: 666px;">  
                <el-option  
                  v-for="item in types"  
                  :key="item.id"  
                  :label="item.title"  
                  :value="item.id"
                  @click="t()"
                  >  
                </el-option>  
              </el-select>


            <!--
                        <el-input v-model="form.typeName" style="width: 370px;" />

            -->

          </el-form-item>
          <el-form-item label="内容" prop="message">
            <el-input :rows="3" v-model="form.message" type="textarea" style="width: 666px;" />
          </el-form-item>

    <!--  
          <el-form-item label="通知范围" prop="message">
            <el-radio-group v-model="form.isAll" >
                <el-radio :label=0>所有</el-radio>
                <el-radio :label=1>指定</el-radio>
            </el-radio-group>          
          </el-form-item>
      -->
          <el-form-item label="指定会员" prop="message" v-if="form.isAll==1">
            <div style="text-align: center">
              <el-transfer
                style="text-align: left; display: inline-block"
                v-model="userList"
                filterable
                :left-default-checked="[2, 3]"
                :right-default-checked="[1]"
                :titles="['未选中', '通知会员']"
                :button-texts="['', '']"
                :format="{
                  noChecked: '${total}',
                  hasChecked: '${checked}/${total}'
                }"
                @change="handleChange"
                :data="noUserList">
                <span slot-scope="{ option }">{{ option.key }} - {{ option.label }}</span>
                <el-button class="transfer-footer" slot="left-footer" size="small">操作</el-button>
                <el-button class="transfer-footer" slot="right-footer" size="small">操作</el-button>
              </el-transfer>
            </div>         
          </el-form-item>








          <el-form-item label="排序">
            <el-input-number v-model="form.weight" style="width: 666px;" />
          </el-form-item>
          
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />

        <!--
         <el-table-column v-if="columns.visible('id')" prop="id" label="主键id" />
        <el-table-column v-if="columns.visible('typeId')" prop="typeId" label="类型外键" />

        <el-table-column v-if="columns.visible('updateTime')" prop="updateTime" label="编辑时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('isDel')" prop="isDel" label="删除(0存/1删)" />
        -->

       
        <el-table-column v-if="columns.visible('heards')" prop="heards" label="标题" />
        <el-table-column v-if="columns.visible('subHeards')" prop="subHeards" label="副标题" />
        <el-table-column v-if="columns.visible('typeName')" prop="typeName" label="通知类型" />
        
          

        <!--
           <el-table-column v-if="columns.visible('message')" prop="message" label="内容" />
        -->
        
        <el-table-column v-if="columns.visible('weight')" prop="weight" label="排序" />
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column v-permission="['admin','yxUserMessage:edit','yxUserMessage:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<style>
  .transfer-footer {
    margin-left: 20px;
    padding: 6px 5px;
  }
</style>

<script>
import crudYxUserMessage from '@/api/yxUserMessage'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '', url: 'api/yxUserMessage', sort: 'id,desc', crudMethod: { ...crudYxUserMessage }})
const defaultForm = { id: null, typeId: null, heards: null, subHeards: null, message: null, weight: null, createTime: null, updateTime: null, isDel: null }
export default {
  name: 'YxUserMessage',
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
  
    return {
      renderFunc(h, option) {
        return <span>{ option.key } - { option.label }</span>;
      },
      
      isAll:0,
      noUserList:[],
      userList:[],
      types:[],
      permission: {
        add: ['admin', 'yxUserMessage:add'],
        edit: ['admin', 'yxUserMessage:edit'],
        del: ['admin', 'yxUserMessage:del']
      },
      rules: {
        typeId: [
          { required: true, message: '类型外键不能为空', trigger: 'blur' }
        ],
        heards: [
          { required: true, message: '消息标题不能为空', trigger: 'blur' }
        ],
        message: [
          { required: true, message: '消息内容不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: '主键id' },
        { key: 'typeId', display_name: '类型外键' },
        { key: 'heards', display_name: '消息标题' },
        { key: 'subHeards', display_name: '消息副标题' },
        { key: 'message', display_name: '消息内容' },
        { key: 'weight', display_name: '排序' },
        { key: 'isDel', display_name: '删除(0存/1删)' }
      ]
    }
  },
  watch: {
  },
  methods: {

    handleChange(value, direction, movedKeys) {
        console.log(value, direction, movedKeys);
    },



    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.id
        delete this.crud.params.typeId
        delete this.crud.params.heards
        delete this.crud.params.subHeards
        delete this.crud.params.message
        delete this.crud.params.weight
        delete this.crud.params.isDel
        delete this.crud.params.types
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
        // 获取所有通知类型
      var that = this
      crudYxUserMessage.getTypes().then(res => {
        that.types = res.types
        that.userList = res.userList
      })

      
      setTimeout(function(){
            },200)
      
    }
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
